<template>
	<view>
		<!-- 状态栏 -->
		<view class="statusBar" :style="'height:'+statusBarHeight+'px'"></view>
		<view class="myheader padding-sm  flex justify-between">
			<view class="font-sub-title">
				<text class="cuIcon-back margin-right-ssm" @click="back()"></text>
				设备基本参数
			</view>
			<view @click="save" class="font-sub-title">
				保存
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item arrow">
				<view class="content">
					<text>身高</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="heightList" :value="heightIndex" @change="bindHeightChange">
						<view>{{heightList[heightIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow">
				<view class="content">
					<text>体重</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="weightList" :value="weightIndex" @change="bindWeightChange">
						<view>{{weightList[weightIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow">
				<view class="content">
					<text>血压</text>
				</view>
				<view class="action text-gray">
					<picker mode="selector" :range="bloodList" :value="bloodIndex" @change="bindBloodChange">
						<view>{{bloodList[bloodIndex]}}</view>
					</picker>
				</view>
			</view>
			<view class="cu-item arrow" @click="goSkinPick">
				<view class="content">
					<text>肤色</text>
				</view>
				<view class="action text-grey">
					<skin :color="skinList[skinIndex]"></skin>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import _config from '@/config/index.js'; // 导入私有配置
	import skin from '@/components/skin/skin.vue'
	import {formatTimeStap} from '@/utils/datetime.js'
	const bandSDK = uni.requireNativePlugin('BandSDK')
	var that;
	export default {
		components:{
			skin
		},
		data() {
			return {
				statusBarHeight: Vue.prototype.StatusBar,
				heightList:[],
				heightIndex:uni.getStorageSync("userHeight")||70,
				weightList:[],
				weightIndex:uni.getStorageSync("userWeight")||30,
				bloodList:['低','正常','轻微偏高','中度偏高','重度高血压'],
				bloodIndex:uni.getStorageSync("userBlood")||1,
				skinList:['#FFFFFF','#FFE8C8','#F4D795','#C88E4E','#624422','#402909'],
				skinIndex:2,
				setting:false,
			}
		},
		onLoad(e){
			that = this;
			if(e.setting=='true'){
				this.setting=true
			}
			for(var i=100;i<=255;i++){
			    this.heightList.push(i+"cm")
			}
			for(var i=30;i<=200;i++){
			    this.weightList.push(i+"kg")
			}
		},
		onShow(){
			var user = uni.getStorageSync("userInfo")
			this.skinIndex= uni.getStorageSync("skinIndex")||2
		},
		onBackPress(event) {
			if(event.from=='backbutton'){
				if(this.setting){
					let main = plus.android.runtimeMainActivity();
					main.moveTaskToBack(false);
				}else{
				return false;	
				}
			}else{
				return false;
			}
			return true;
		},
		methods: {
			back(){
				if(this.setting){
					let main = plus.android.runtimeMainActivity();
					main.moveTaskToBack(false);
				}else{
					uni.navigateBack();
				}
			},
			bindHeightChange: function(e) {
				this.heightIndex = e.target.value
			},
			bindWeightChange: function(e) {
				this.weightIndex = e.target.value
			},
			bindBloodChange: function(e) {
				this.bloodIndex = e.target.value
			},
			save(){
				bandSDK.connectState(res => {
					if (res == "disconnected") {
						uni.showToast({
							"title":"手环未连接",
							"position":"bottom",
							"icon":"none"
						})
						return;
					}
				})
				var user =uni.getStorageSync("userInfo")
				var sex,age;
				if(user.SEX!=undefined&&user.SEX!=null&&user.SEX!=''){
					sex = user.SEX -1
				}else{
					sex=0
				}
				if(user.AGE!=undefined&&user.AGE!=null&&user.AGE!=''){
					age = user.AGE
				}else{
					age=18
				}
				var height = that.heightList[that.heightIndex]
				var weight = that.weightList[that.weightIndex]
				bandSDK.settingUserInfo({
					'height':height.substring(0,height.length-2),
					'weight':weight.substring(0,weight.length-2),
					'sex':sex,
					'age':age
				},res=>{
					if(res=='success'){
						uni.setStorageSync("userHeight",that.heightIndex)
						uni.setStorageSync("userWeight",that.weightIndex)
						bandSDK.settingBloodRange({
							level:that.bloodIndex
						},res=>{
							if(res=='success'){
								uni.setStorageSync("userBlood",that.bloodIndex)
								uni.uploadFile({
									url:_config.ipAddress+"app/updateUserInfo",
									header:{
										"token":uni.getStorageSync("token"),
										"currentTime":formatTimeStap(new Date())
									},
									sizeType: ['compressed'],  
									filePath:'',
									name:'',
									formData:{
										flag:'device',
										height:height.substring(0,height.length-2),
										weight:weight.substring(0,weight.length-2),
										blood:that.bloodIndex,
										skin:uni.getStorageSync("skinIndex")||2
									},
									success:(res)=>{
										res = JSON.parse(res.data)
										if(res.code==1){
											uni.showToast({title:"保存成功！"});
											if(that.setting){
												setTimeout(function(){
													uni.switchTab({
														url:'../../index/index'
													})
												},1000)
											}
										}else{
											uni.showToast({title:res.message, position:"bottom",icon:"none"});
										}
									},
									fail(res) {
										
									}
								})
							}
						})
					}
				})
			},
			goSkinPick(){
				uni.navigateTo({
					url:"/pages/user/userInfo/skinpick"
				})
			}
		}
	}
</script>

<style>
	
</style>
